# -*- coding: utf-8 -*-

from django.shortcuts import render
from django.views.generic.base import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from .models import Article, Catagory, Tag

# Create your views here.


class ArticleListView(View):
    """
    文章列表页
    """
    def get(self, request, category):

        all_article = Article.objects.filter(category__name=category)
        print(all_article)

        # 对文章进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_article, 3, request=request)

        all_article = p.page(page)

        return render(request, 'Article_list.html', {
            "all_article": all_article,
            "category": category,
        })


class ArticleDetailView(View):
    """
    文章详情页
    """
    def get(self, request, category, article_id):

        article = Article.objects.get(id=int(article_id))

        return render(request, 'Article.html', {
            "article": article,
            "category": category,
        })
